package com.example.wechat.activity;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.wechat.R;
import com.example.wechat.adapter.LocationAdapter;
import java.util.ArrayList;
import java.util.List;

public class LocationPickerActivity extends AppCompatActivity implements LocationAdapter.OnLocationClickListener {
    private RecyclerView recyclerView;
    private LocationAdapter adapter;
    private List<String> locations;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location_picker);

        // 设置工具栏
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("选择位置");

        // 初始化RecyclerView
        recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        locations = new ArrayList<>();
        adapter = new LocationAdapter(locations, this);
        recyclerView.setAdapter(adapter);

        // 加载位置数据
        loadLocations();
    }

    private void loadLocations() {
        // 这里使用测试数据
        locations.clear();
        locations.add("北京市");
        locations.add("上海市");
        locations.add("广州市");
        locations.add("深圳市");
        locations.add("杭州市");
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onLocationClick(String location) {
        // 返回选中的位置
        getIntent().putExtra("location", location);
        setResult(RESULT_OK, getIntent());
        finish();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 